/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.bridge.major;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ru.jSkyRoom.finder.FinderByFormId;
import ru.jSkyRoom.interfaces.major.Keyword;
import ru.jSkyRoom.interfaces.major.Major;
import ru.jSkyRoom.interfaces.major.Recipe;
import skyproc.COBJ;
import skyproc.Condition;
import skyproc.FormID;
import skyproc.SubFormInt;

/**
 *
 * @author Rakev
 */
public class RecipeBridge extends MajorBridge<COBJ> implements Recipe {

    public RecipeBridge(COBJ adapting) {
        super(adapting);
    }

    @Override
    public Collection<Condition> getConditions() {
        return Collections.unmodifiableList(getRawObject().getConditions());
    }

    @Override
    public void addCondition(Condition condition) {
        getRawObject().addCondition(condition);
    }

    @Override
    public void removeCondition(Condition condition) {
        getRawObject().removeCondition(condition);
    }

    @Override
    public Map<Major, Integer> getIngredients() {
        List<SubFormInt> records = getRawObject().getIngredients();
        Map<Major, Integer> result = new HashMap<>(records.size());
        FinderByFormId<Major> finder = getFinderFactory().createMajorFinder();

        for (SubFormInt rawItem : records) {
            Major item = finder.find(rawItem.getForm());
            result.put(item, rawItem.getNum());
        }
        return Collections.unmodifiableMap(result);
    }

    @Override
    public void putIngredient(Major item, int count) {
        FormID itemID = item.getForm();
        getRawObject().addIngredient(itemID, count);
    }

    @Override
    public void removeIngredient(Major item) {
        FormID id = getIdProducer().produce(item);
        getRawObject().removeIngredient(id);
    }

    @Override
    public Major getOutputItem() {
        FormID id = getRawObject().getResultFormID();
        return getFinderFactory().createMajorFinder().find(id);
    }

    @Override
    public void setOutputItem(Major item) {
        FormID id = getIdProducer().produce(item);
        getRawObject().setResultFormID(id);
    }

    @Override
    public Keyword getBenchKeyword() {
        FormID id = getRawObject().getBenchKeywordFormID();
        return getFinderFactory().createKeywordFinder().find(id);
    }

    @Override
    public void setBenchKeyword(Keyword keyword) {
        FormID id = getIdProducer().produce(keyword);
        getRawObject().setBenchKeywordFormID(id);
    }

    @Override
    public int getOutputCount() {
        return getRawObject().getOutputQuantity();
    }

    @Override
    public void setOutputCount(int quantity) {
        getRawObject().setOutputQuantity(quantity);
    }
}
